package com.hundun.vision.biz.utils;

import com.hundun.vision.biz.dto.KlineBarDTO;
import org.knowm.xchart.*;

import javax.swing.*;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：RuoChen
 * @date ：08:54 2024/12/27
 * @description：图表服务
 */
public class ChartUtils {

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    public static void createChart(List<KlineBarDTO> klineBars) {
        // 创建价格趋势图
        XYChart priceChart = createPriceChart(klineBars);
        // 创建成交量趋势图
        CategoryChart volumeChart = createVolumeChart(klineBars);
        try {
            // 保存价格趋势图
            BitmapEncoder.saveBitmap(priceChart, "./price_trend", BitmapEncoder.BitmapFormat.PNG);
            // 保存成交量趋势图
            BitmapEncoder.saveBitmap(volumeChart, "./volume_trend", BitmapEncoder.BitmapFormat.PNG);
        } catch (Exception e) {
            e.printStackTrace();
        }
//        // 将两个图表添加到一个窗口
//        JPanel panel = new JPanel(new GridLayout(2, 1));
//        panel.add(new XChartPanel<>(priceChart));
//        panel.add(new XChartPanel<>(volumeChart));
//        // 显示组合图表
//        JFrame frame = new JFrame("Doge Analysis");
//        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//        frame.add(panel);
//        frame.pack();
//        frame.setVisible(true);
    }

    /**
     * 绘制价格趋势图
     *
     * @param klineBars
     */
    public static XYChart createPriceChart(List<KlineBarDTO> klineBars) {
        List<Date> timestamps = klineBars.stream()
                .map(s -> new Date(s.getStartTimestamp()))
                .collect(Collectors.toList());
        List<Double> openPrices = klineBars.stream().map(bar -> bar.getOpenPrice().doubleValue()).collect(Collectors.toList());
        List<Double> closePrices = klineBars.stream().map(bar -> bar.getClosePrice().doubleValue()).collect(Collectors.toList());
        List<Double> highPrices = klineBars.stream().map(bar -> bar.getHighPrice().doubleValue()).collect(Collectors.toList());
        List<Double> lowPrices = klineBars.stream().map(bar -> bar.getLowPrice().doubleValue()).collect(Collectors.toList());
        XYChart chart = new XYChartBuilder()
                .width(800)
                .height(400)
                .title("Price Trend")
                .xAxisTitle("Time")
                .yAxisTitle("Price")
                .build();
        chart.getStyler().setDatePattern("yyyy-MM-dd HH:mm");
        chart.getStyler().setLegendPosition(org.knowm.xchart.style.Styler.LegendPosition.InsideNE);
        chart.addSeries("Open Price", timestamps, openPrices);
        chart.addSeries("Close Price", timestamps, closePrices);
        chart.addSeries("High Price", timestamps, highPrices);
        chart.addSeries("Low Price", timestamps, lowPrices);
        return chart;
    }

    /**
     * 绘制成交量趋势图
     *
     * @param klineBars
     */
    public static CategoryChart createVolumeChart(List<KlineBarDTO> klineBars) {
        List<Date> timestamps = klineBars.stream()
                .map(s -> new Date(s.getStartTimestamp()))
                .collect(Collectors.toList());
        List<Double> volumes = klineBars.stream().map(bar -> bar.getVolume().doubleValue()).collect(Collectors.toList());
        CategoryChart chart = new CategoryChartBuilder()
                .width(800)
                .height(400)
                .title("Volume Trend")
                .xAxisTitle("Time")
                .yAxisTitle("Volume")
                .build();
        chart.getStyler().setDatePattern("yyyy-MM-dd HH:mm");
        chart.getStyler().setLegendVisible(false);
        chart.addSeries("Volume", timestamps, volumes);
        return chart;
    }
}
